package com.jnxx.task.service.impl;

import com.jnxx.task.domain.Okrcard;
import com.jnxx.task.domain.Task;
import com.jnxx.task.mapper.TaskMapper;
import com.jnxx.task.service.IOkrcardService;
import com.jnxx.basic.service.impl.BaseServiceImpl;
import com.jnxx.task.service.ITaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class OkrcardServiceImpl extends BaseServiceImpl<Okrcard> implements IOkrcardService {

    @Autowired
    private TaskMapper taskMapper;

    @Autowired
    private ITaskService taskService;

    @Override
    public void del(Long id) {
        List<Task> tasks = taskMapper.findByCardId(id);
        for (Task task : tasks) {
            taskService.del(task.getId());
        }
        super.del(id);
    }

    @Override
    public void update(Okrcard okrcard) {
        Long id = okrcard.getId();
        String cardName = okrcard.getCardName();
        List<Task> taskList = taskMapper.findByCardId(id);
        for (Task task : taskList) {
            String projectDetails = task.getProjectDetails();
            String[] parts = projectDetails.split("/");

            if (parts.length >= 2) {
                // 将第二个子字符串替换为新card名字
                parts[2] = cardName;
                // 使用join方法拼接字符串
               String newProjectDetails = String.join("/", parts);
               task.setProjectDetails(newProjectDetails);
               taskMapper.update(task);
            } else {
                // 处理字符串不包含足够子字符串的情况
                System.out.println("字符串不包含足够的子字符串");
            }
        }

        super.update(okrcard);
    }
}
